package com.zwy.space.service;


import com.zwy.space.domain.Blog;
import com.zwy.space.domain.BlogType;
import com.zwy.space.mapper.BlogMapper;
import com.zwy.space.queryVo.BlogDetailVo;
import com.zwy.space.queryVo.BlogVo;
import com.zwy.space.queryVo.IndexVo;
import com.zwy.space.queryVo.RecommendVo;
import com.zwy.space.queryVo.admin.SearchBlogVo;
import com.zwy.space.queryVo.admin.ShowBlog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class BlogService {
    @Autowired
    private BlogMapper blogMapper;

    public List<IndexVo> findBlogsByPage(Integer pageNum) {
        int start=(pageNum-1)*5;
        int rows=5;
        return blogMapper.findBlogsByPage(start,rows);
    }

    public List<RecommendVo> findRecommendBlogs() {
        return blogMapper.findRecommendBlogs();
    }

    public BlogDetailVo findBlogById(int blogId) {
        return blogMapper.findBlogById(blogId);
    }

    public List<IndexVo> searchBlogs(String query) {
        return blogMapper.searchBlogs(query);
    }

    public List<BlogVo> findAllBlogs() {
        return blogMapper.findAllBlogs();
    }

    public List<BlogType> findAllTypes() {
        return blogMapper.findAllTypes();
    }

    public List<BlogVo> findBlogsBySerach(SearchBlogVo searchBlogVo) {
        return blogMapper.findBlogsBySerach(searchBlogVo);
    }

    public int saveBlog(Blog blog) {
        blog.setCreateTime(new Date());
        blog.setUpdateTime(new Date());
        blog.setViews(0);
        blog.setCommentCount(0);
        return blogMapper.saveBlog(blog);
    }

    public Blog editBlogById(int blogId) {
        return blogMapper.editBlogById(blogId);
    }

    public int updateBlog(ShowBlog showBlog) {
        showBlog.setUpdateTime(new Date());
        return blogMapper.updateBlog(showBlog);
    }

    public void deleteBlog(int id) {
         blogMapper.deleteBlog(id);
    }
}
